﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Specialized;
using System.IO;

public partial class Admin_Tintuc : System.Web.UI.Page
{
    DADataContext da = new DADataContext();
    XL kn = new XL();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (string.IsNullOrEmpty(Convert.ToString(Session["user"])))
            {
                Response.Redirect("/Online/Dangnhap/");
            }
            else
            {
                var mcn = (from cn in da.Chucnangs where cn.Tenchucnang == "Quản lý tin tức" select cn.Machucnang).FirstOrDefault();
                var kt = (from a in da.TaikhoanChucnangs where (a.Taikhoan == Session["user"].ToString() && a.Machucnang == mcn) select a).Count();
                if (kt == 1)
                {
                    LoadGV();
                    if (string.IsNullOrEmpty(Convert.ToString(Request.QueryString["m"])))
                    {
                        btnThem.Enabled = true;
                        btnSua.Enabled = false;
                    }
                    else
                    {
                        int m = int.Parse(Request.QueryString["m"]);
                        var select = (from t in da.Tintucs where t.Matin == m select t).FirstOrDefault();
                        txttd.Text = select.Tieude;
                        txttt.Text = select.Tomtat;
                        fcknd.Value = select.Noidung;
                        if (select.Hienthi == 1)
                            ht.Checked = true;
                        else kht.Checked = true;
                        btnThem.Enabled = false;
                    }
                }
                else
                {
                    Response.Redirect("/Online/Admin/Default.aspx");
                }
            }
        }
    }
    protected void btnThem_Click(object sender, EventArgs e)
    {
        try
        {
            if (txttd.Text == "" || txttt.Text == "" || fcknd.Value == "")
            {
                tb.Text = "Nhập đủ các thông tin!";
            }
            else
            {
                string td = txttd.Text.Trim();
                string tt = txttt.Text.Trim();
                string nd = fcknd.Value.Trim();
                string ha = "/Online/Admin/Tintuc/" + file.FileName;
                DateTime dt = DateTime.Parse(System.DateTime.Now.ToShortDateString() + " " + System.DateTime.Now.ToLongTimeString());
                int hienthi = 0, mt = 0;
                if (ht.Checked == true) hienthi = 1;
                else hienthi = 0;
                var layma = (from t in da.Tintucs select t.Matin).Count();
                if (layma == 0)
                {
                    mt = 1;
                }
                else
                {
                    var tangma = (from a in da.Tintucs orderby a.Matin descending select a.Matin).FirstOrDefault();
                    mt = int.Parse(tangma.ToString()) + 1;
                }
                Tintuc tintuc = new Tintuc();
                tintuc.Matin = mt;
                tintuc.Tieude = td;
                tintuc.Tomtat = tt;
                tintuc.Alisa = kn.ConvertToUnSign(td);
                tintuc.Anhdaidien = ha;
                tintuc.Noidung = nd;
                tintuc.Ngaydang = dt;
                tintuc.Hienthi = hienthi;
                tintuc.Taikhoan = Session["user"].ToString();
                da.Tintucs.InsertOnSubmit(tintuc);
                da.SubmitChanges();
                file.SaveAs(Server.MapPath("/Online/Admin/Tintuc/") + file.FileName);
                tb.Text = "Thêm thành công!";
                Reset();
                LoadGV();
            }
        }
        catch (Exception ax)
        {
            tb.Text = ax.Message;
        }
    }
    protected void btnSua_Click(object sender, EventArgs e)
    {
        try
        {
            if (txttd.Text == "" || txttt.Text == "" || fcknd.Value == "")
            {
                tb.Text = "Nhập đủ các thông tin!";
            }
            else
            {
                string td = txttd.Text.Trim();
                string tt = txttt.Text.Trim();
                string nd = fcknd.Value.Trim();

                int ma = int.Parse(Request.QueryString["m"]);

                var up = (from p in da.Tintucs where p.Matin == ma select p).Single();
                up.Tieude = txttd.Text.Trim();
                up.Tomtat = txttt.Text.Trim();
                up.Alisa = kn.ConvertToUnSign(td);
                up.Noidung = fcknd.Value;
                if (ht.Checked == true)
                    up.Hienthi = 1;
                else up.Hienthi = 0;

                if (ckanh.Checked == false)
                {
                    da.SubmitChanges();
                    Response.Redirect("/Online/Admin/Tintuc.aspx");

                    //tb.Text = "Sửa thành công!";
                    //Reset();
                    //LoadGV();
                }
                else
                {
                    if (file.HasFile)
                    {
                        string ha = "/Online/Admin/Tintuc/" + file.FileName;
                        file.SaveAs(Server.MapPath(ha));
                        FileInfo info = new FileInfo(Server.MapPath(up.Anhdaidien));
                        info.Delete();
                        da.SubmitChanges();
                        Response.Redirect("/Online/Admin/Tintuc.aspx");
                        //tb.Text = "Sửa thành công!";
                        //Reset();
                        //LoadGV();
                    }
                    else
                        tb.Text = "Hãy chọn ảnh mới để thay ảnh";
                }

            }
        }
        catch (Exception ax)
        {
            tb.Text = ax.Message;
        }
    }
    protected void btnXoa_Click(object sender, EventArgs e)
    {
        StringCollection idConnllection = new StringCollection();
        string strids = string.Empty;

        for (int i = 0; i < GVTT.Rows.Count; i++)
        {
            CheckBox ck = (CheckBox)GVTT.Rows[i].Cells[0].FindControl("ckq1");
            if (ck != null)
            {
                if (ck.Checked == true)
                {
                    Label lb = (Label)GVTT.Rows[i].Cells[1].FindControl("Ma");
                    strids = lb.Text;
                    idConnllection.Add(strids);
                }
            }
        }
        if (idConnllection.Count > 0)
        {
            string iDS = ""; int a;
            foreach (string id in idConnllection)
            {
                iDS += id + ",";
            }
            string[] mang = iDS.Split(',');
            for (int i = 0; i < mang.Length - 1; i++)
            {
                a = int.Parse(mang[i]);
                var xoa = da.Tintucs.SingleOrDefault(p => p.Matin == a);
                if (xoa != null)
                {
                    da.Tintucs.DeleteOnSubmit(xoa);
                    da.SubmitChanges();
                }
            }
            tb.Text = "Xóa thành công!";
            LoadGV();
        }
        else
            tb.Text = "Không bản ghi nào được chọn để xóa!";
    }
    protected void btnHuy_Click(object sender, EventArgs e)
    {
        Response.Redirect("/Online/Admin/Tintuc.aspx");

    }
    void LoadGV()
    {
        var loadtin = from t in da.Tintucs
                      orderby t.Matin descending
                      select new { t.Matin, t.Tieude, t.Taikhoan, t.Ngaydang, Hienthi = t.Hienthi.Value.Equals(1) ? "Hiển thị" : "Không hiển thị" };
        GVTT.DataSource = loadtin;
        GVTT.DataBind();
    }
    void Reset()
    {
        txttd.Text = "";
        txttt.Text = "";
        fcknd.Value = "";
    }
}
